package com.flow.manager.action;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import org.apache.struts2.ServletActionContext;

import com.flow.model.BackInfo;
import com.flow.model.NumberIndex;
import com.flow.model.PagerModel;
import com.flow.model.UserBasic;
import com.flow.service.INumberIndexService;
import com.flow.service.IUserBasicService;
import com.flow.tools.StringTools;

/**
 * 会员基本信息Action
 * 
 * @author wang
 * @2015-12-6 UserBasicAction.java
 * 
 */
public class UserBasicAction {

	private BackInfo back = new BackInfo();

	/**
	 * 服务引用
	 */
	private IUserBasicService<UserBasic> ubService;

	private INumberIndexService<NumberIndex> niService;

	/**
	 * 获取用户列表
	 */
	public String findAllUserBasic() {

		HttpServletRequest request = ServletActionContext.getRequest();
		String condition = request.getParameter("searchCondition");

		try {

			Map<String, Object> map = new HashMap<String, Object>();
			map.put("ubName", condition);
			map.put("ubPhone", condition);
			map.put("ubEmail", condition);
			PagerModel pager = ubService.findUserBasic(map);
			request.setAttribute("pager", pager);
			request.setAttribute("map", map);
			return null;

		} catch (Exception e) {

			e.printStackTrace();

		}

		return "ERROR";
	}

	/**
	 * 登录方法
	 */
	public String login() {

		HttpServletRequest request = ServletActionContext.getRequest();
		String loginName = request.getParameter("loginName");
		String loginPwd = request.getParameter("loginPwd");
		request.getSession().removeAttribute("ADMINID");
		try {

			UserBasic ub = ubService.login(loginName);
			if (null == ub) {
				back.setBackMsg("用户名不存在！");
				System.out.println(back.getBackMsg());
				return "LOGIN";
			} else if (!ub.getUbPwd().equals(loginPwd)) {
				back.setBackMsg("密码错误！");
				System.out.println(back.getBackMsg());
				return "LOGIN";
			} else {
				request.getSession().setAttribute("ADMINID", ub.getUbId());
				return "SUCCESS";
			}
		} catch (Exception e) {

			e.printStackTrace();

		}

		return "ERROR";
	}

	/**
	 * 编辑用户信息
	 */
	public String inputUpdate(){
		
		HttpServletRequest request = ServletActionContext.getRequest();
		String ubId = request.getParameter("ubId");
		
		try {
			
			UserBasic ub = ubService.findUserBasicById(ubId);
			request.setAttribute("ub", ub);
			return "SUCCESS";
			
		} catch (Exception e) {

			e.printStackTrace();
			
		}
		
		return "ERROR";
	}
	
	/**
	 *  更新用户
	 */
	public String updateUserBasic() {

		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("ubId");
		String pwd = request.getParameter("ubPwd");
		String telphone = request.getParameter("ubPhone");
		String email = request.getParameter("ubEmail");
		String status = request.getParameter("ubStatus");
		status = !StringTools.nullOrEmpty(status) || null == status ? "0" : status;

		try {

			UserBasic userBasic = new UserBasic(id, telphone, email, pwd, Integer.parseInt(status));
			if (ubService.updateUserBasic(userBasic)) {
				return "SUCCESS";
			}

		} catch (Exception e) {

			e.printStackTrace();

		}

		return "ERROR";

	}

	/**
	 * 添加用户
	 */
	public String insertUserBasic() {

		HttpServletRequest request = ServletActionContext.getRequest();
		ServletActionContext.getResponse().setCharacterEncoding("utf-8");

		try {

			String id = request.getParameter("ubId");
			String pwd = request.getParameter("ubPwd");
			String telphone = request.getParameter("ubPhone");
			String email = request.getParameter("ubEmail");
			String status = request.getParameter("ubStatus");
			status = !StringTools.nullOrEmpty(status) || null == status ? "0"
					: status;
			String number = niService.getNumberIndex();
			UserBasic userBasic = new UserBasic(id + number, telphone, email,
					pwd, Integer.parseInt(status));

			if (ubService.insertUserBasic(userBasic)) {
				if (niService.insertNumberIndex(new NumberIndex(userBasic
						.getUbId(), number, 0)))
					return "SUCCESS";
			}

		} catch (Exception e) {

			e.printStackTrace();

		}

		return "ERROR";
	}

	/**
	 * 验证电话号码是否可用
	 */
	public String validateTelphone() {

		HttpServletRequest request = ServletActionContext.getRequest();
		ServletActionContext.getResponse().setCharacterEncoding("utf-8");
		PrintWriter out = null;
		String telphone = request.getParameter("ubPhone");

		try {

			out = ServletActionContext.getResponse().getWriter();
			if (ubService.validateTelphone(telphone)) {
				back.setBackCode("000");
				back.setBackMsg("可以使用.");
			} else {
				back.setBackCode("001");
				back.setBackMsg("电话号已存在.");
			}
			JSON json = JSONSerializer.toJSON(back);
			out.print(json);
			out.flush();
			out.close();
			return null;

		} catch (Exception e) {

			e.printStackTrace();

		}

		return "ERROR";
	}

	/**
	 * 验证用户邮箱
	 */
	public String validateEmail() {

		HttpServletRequest request = ServletActionContext.getRequest();
		ServletActionContext.getResponse().setCharacterEncoding("utf-8");
		PrintWriter out = null;
		String email = request.getParameter("ubEmail");

		try {

			out = ServletActionContext.getResponse().getWriter();
			if (ubService.validateEmail(email)) {
				back.setBackCode("000");
				back.setBackMsg("可以使用.");
			} else {
				back.setBackCode("001");
				back.setBackMsg("邮箱已存在.");
			}
			JSON json = JSONSerializer.toJSON(back);
			out.print(json);
			out.flush();
			out.close();
			return null;

		} catch (Exception e) {

			e.printStackTrace();

		}

		return "ERROR";
	}

	public void setUbService(IUserBasicService<UserBasic> ubService) {
		this.ubService = ubService;
	}

	public BackInfo getBack() {
		return back;
	}

	public void setBack(BackInfo back) {
		this.back = back;
	}

	public void setNiService(INumberIndexService<NumberIndex> niService) {
		this.niService = niService;
	}

}
